package com.lz.frame.config.security;


import com.lz.frame.domain.entity.User;
import com.lz.frame.domain.mapper.UserMapper;
import com.lz.frame.filter.JwtUtil;
import com.lz.frame.utils.SecurityUtil;
import com.xinchili.UserLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class MyUserLog implements UserLogUtil {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SecurityUtil securityUtil;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public String getUserId(HttpServletRequest httpServletRequest) {
        return securityUtil.getId();
    }

    /**
     * 根据userID获取用户token
     *
     * @param s
     * @return
     */
    @Override
    public String getUserToken(String s) {
        User userByUserId = userMapper.getUserByUserId(s);
        MyUserDetails02 userDetails = (MyUserDetails02) userDetailsService.loadUserByUsername(userByUserId.getUsername());
        String token = jwtUtil.generateAccessToken(userDetails);
        return token;
    }
}
